package com.fengli.task.controller;

import com.fengli.task.entity.TaskEntity;
import com.fengli.task.service.TaskService;
import com.fengli.task.uitl.ApplicationUtil;
import com.fengli.task.uitl.Result;
import com.fengli.task.uitl.enumType.Code;
import com.fengli.task.uitl.exception.ParamException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

@RestController
public class TaskController {
    @Autowired
    TaskService taskService;

    /**
     * 添加任务
     *
     * @return
     */
    public Result addTask(Integer importanceValue, String content, String date, String createTime, String startTime, String endTime,
                          String realEndTime,
                          boolean isDone,
                          boolean delete) {
        //？date,dateTime可以总动转换为date,dateTime吗
        /*todo 校验参数*/
        if (StringUtils.isEmpty(content)) {
            throw new ParamException("任务内容为空");
        }
        if (StringUtils.isEmpty(date)) {
            throw new ParamException("密码为空");
        }

        /*todo 调用service层*/

        /*todo 返回结果*/
        return null;
    }

    @ResponseBody
    @RequestMapping(value = "/addTask", method = RequestMethod.POST)
    public Result addTask(@RequestBody TaskEntity task, BindingResult bindingResult){
        /*查看校验结果*/
        if(bindingResult.hasErrors()){
            List<ObjectError> list =bindingResult.getAllErrors();
            throw new ParamException(list.get(0).getDefaultMessage());
        }

        /* 调用service层*/
        taskService.addTask(task);
        Result result= new Result("添加任务成功", Code.SUCCESS, null);
        /*返回结果*/
        return result;
    }

    /**
     * 修改任务
     * @param taskEntity
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/updateTask", method = RequestMethod.POST)
    @ResponseBody
    public Result updateTask(@RequestBody TaskEntity taskEntity,BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
            List<ObjectError> list =bindingResult.getAllErrors();
            throw new ParamException(list.get(0).getDefaultMessage());
        }
        taskService.modifyTask(taskEntity);
        Result result= new Result("添加任务成功", Code.SUCCESS, null);
        /*返回结果*/
        return result;
    }

    /**
     * 保存任务
     * @param taskEntity
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/saveTask", method = RequestMethod.POST)
    @ResponseBody
    public Result saveTask(@RequestBody TaskEntity taskEntity, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            List<ObjectError> list =bindingResult.getAllErrors();
            throw new ParamException(list.get(0).getDefaultMessage());
        }
        taskService.saveTask(taskEntity);
        Result r = new Result("添加任务成功",Code.SUCCESS,null);
        return  r;
    }

    /**
     *  查询tasks
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/pullDataFromBackEnd", method = RequestMethod.GET)
    public Result queryData(String userId) {
        if(StringUtils.isEmpty(userId)){
            throw new ParamException("userId为空");
        }
        return taskService.queryData(userId);
    }

    /**
     * todo 生成日志
     */
}
